import setupWebViewJavascriptBridge from '../javascriptBridge'

/**
 * 调用这个js-api进行登陆操作，获得jsSession已进行后续的操作
 * @since 1.0.0
 * @param {object} appkey
 * @returns {Object} 返回一个登陆是否成功的信息
 * @example
 * 
 * login({appKey: '000000'}).then(res => {}).catch(err => {})
 */
function login(api) {
    return new Promise((resolve, reject) => {
        setupWebViewJavascriptBridge(() => {
            window.WebViewJavascriptBridge.callHandler('login', {'appKey': api.appKey}, (response) => {
                if (typeof response === 'string') {
                    response = JSON.parse(response)
                }
                if (response.status === 0) {
                    sessionStorage.setItem('jsSession', response.result.session)
                    resolve(response)
                } else {
                    reject(response)
                }
            })
        })
    })
}

export default login